package cn.zhl.algorithm._3_并查集;

import java.util.Arrays;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-06-11 18:55
 **/
public abstract class UnionFind {
	protected int[] parent;
	public UnionFind(int capacity){
		parent = new int[capacity];
		for(int i = 0;i<capacity;i++){
			parent[i] = i;
		}
	}

	public abstract int union(int v1,int v2);

	public abstract int find(int v);

	public boolean isSame(int v1,int v2){
		return find(v1) == find(v2);
	}

	protected void rangeCheck(int v){
		if(v < 0 || v >= parent.length){
			throw new RuntimeException("范围超出");
		}
	}

	@Override
	public String toString() {
		return "UnionFind{" +
			"parent=" + Arrays.toString(parent) +
			'}';
	}
}
